//
//  XiuGaiYongHuMingViewController.m
//  JuBoCRM
//
//  Created by iMac on 2024/2/23.
//  Copyright © 2024 iningKe. All rights reserved.
//

#import "XiuGaiYongHuMingViewController.h"

@interface XiuGaiYongHuMingViewController ()

@property (weak, nonatomic) IBOutlet TPKeyboardAvoidingScrollView *mainScrollView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;
@property (weak, nonatomic) IBOutlet UITextField *nametf;
@property (weak, nonatomic) IBOutlet UILabel *numLabel;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomSpace;
@end

@implementation XiuGaiYongHuMingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.customNavBar.title = @"修改名字";
 
    _topSpace.constant = NavaBarHeight;
    
    _bottomSpace.constant = ScreenBottom+20;
    
    if (@available(iOS 11.0, *)) {
        self.mainScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    _nametf.text = GetHandleString(USERINFO, @"realname");
    _numLabel.text = [NSString stringWithFormat:@"%ld/30",_nametf.text.length];
      [_nametf addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
}
- (void)textFieldDidChange:(UITextField *)textField{
    CGFloat maxLength = 30;
    NSString *toBeString = textField.text;
    
    //获取高亮部分
    UITextRange *selectedRange = [textField markedTextRange];
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    if (!position || !selectedRange)
    {
        if (toBeString.length > maxLength)
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxLength];
            if (rangeIndex.length == 1)
            {
                textField.text = [toBeString substringToIndex:maxLength];
            }
            else
            {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
                textField.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
    
    _numLabel.text = [NSString stringWithFormat:@"%ld/30",textField.text.length];
}

//确认修改
- (IBAction)xiuGai:(UIButton *)sender {
    
    [self.view endEditing:YES];
    
    if(_nametf.text.length == 0){
        [ZZPUtils showWarning:_nametf.placeholder];
        return;
    }
    
    NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
    [paraDic setValue:_nametf.text forKey:@"realname"];
    [paraDic setValue:GetHandleString(USERINFO, @"mobile") forKey:@"username"];
    [[ZZPRequest getWebUtil] postJsonNetworkRequestURLString:@"/adminUser/updateUser" andParas:paraDic andShowHud:YES andTransferGottenData:^(id info, NSError *error) {
        if(!error){
            if(_changeResultBlock){
                _changeResultBlock(_nametf.text);
            }
            [ZZPUtils showWarning:@"修改成功"];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"changeName" object:_nametf.text];
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }
    }];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
